package org.jeecgframework.web.service.impl;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ZyCommonAddressServiceI;
import org.jeecgframework.web.system.pojo.base.ZyCommonAddressEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.io.Serializable;

@Service("zyCommonAddressService")
@Transactional
public class ZyCommonAddressServiceImpl extends CommonServiceImpl implements ZyCommonAddressServiceI {

    private static final Logger logger = Logger.getLogger(ZyCommonAddressServiceImpl.class);

    public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
        ZyCommonAddressEntity ct = (ZyCommonAddressEntity)entity;
		this.doDelSql((ZyCommonAddressEntity)entity);
        if("1".equals(ct.getIsDefault())){
            List<ZyCommonAddressEntity>  address = getAddressByUser(ct.getBaseUser().getUserName());
            if(address.size() > 0){
                address.get(0).setIsDefault("1");
                updateEntitie(address.get(0));
            }
        }
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((ZyCommonAddressEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((ZyCommonAddressEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @return
	 */
 	public boolean doAddSql(ZyCommonAddressEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @return
	 */
 	public boolean doUpdateSql(ZyCommonAddressEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @return
	 */
 	public boolean doDelSql(ZyCommonAddressEntity t){
	 	return true;
 	}

    @Override
    public void addNewAddress(ZyCommonAddressEntity address) {
        try{
            if(StringUtil.isEmpty(address.getId()) && !checkIfCanAdd(address.getBaseUser().getUserName())){
                return;
            }
        }catch(Exception e){
            logger.error("地址信息关联的用户为空"+e.getMessage());
        }
        if(StringUtil.isEmpty(address.getId())){
            save(address);
        }else{
            updateEntitie(address);
        }
        if("1".equals(address.getIsDefault())){
            setDefault(address.getId());
        }
    }

    @Override
    public boolean checkIfCanAdd(String userName) {
//        Long currentAddressCount = getCountForJdbc("select count(1) from zy_common_address ad,t_s_base_user tu where ad.base_id = tu.id and tu.username='"+oConvertUtils.getString(userName)+"'" );
        return getAddressByUser(userName).size() < 10;
    }

    @Override
    public void setDefault(String id) {
        ZyCommonAddressEntity address = get(ZyCommonAddressEntity.class,id);
        if(null != address){
            address.setIsDefault("1");
            saveOrUpdate(address);
            updateBySqlString("update zy_common_address set is_default='0' where id <>'"+id+"'");
        }
    }

    public List<ZyCommonAddressEntity> getAddressByUser(String userName) {
        return findHql("from ZyCommonAddressEntity cd where cd.baseUser.userName=? order by updateDate desc,createDate desc",userName);
    }

    public ZyCommonAddressEntity getDefaultAddress(String userName) {
 		List<ZyCommonAddressEntity> cts = findHql("from ZyCommonAddressEntity cd where cd.baseUser.userName=? and cd.isDefault=1 order by updateDate desc,createDate desc",userName);
        return cts.size()>0?cts.get(0):new ZyCommonAddressEntity();
    }

    /**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyCommonAddressEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{contact_name}",String.valueOf(t.getContactName()));
 		sql  = sql.replace("#{phone}",String.valueOf(t.getPhone()));
 		sql  = sql.replace("#{mail}",String.valueOf(t.getMail()));
 		sql  = sql.replace("#{fax}",String.valueOf(t.getFax()));
 		sql  = sql.replace("#{province}",String.valueOf(t.getProvince()));
 		sql  = sql.replace("#{full_address}",String.valueOf(t.getFullAddress()));
 		sql  = sql.replace("#{is_default}",String.valueOf(t.getIsDefault()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}